package com.rjj.wlqq.listen.handle.config;

import com.rjj.wlqq.listen.handle.GlobleHandle;
import com.rjj.wlqq.listen.handle.SystemUrlFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;

import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author rjj
 */
//@Configuration //上下两个注解都可以使用
@SpringBootConfiguration
public class FilterConfig {

    private final Logger log = LoggerFactory.getLogger(FilterConfig.class);

    /**
     * 第一种方式
     * 配置过滤器
     * 注册过滤器
     */
    @Bean
    public FilterRegistrationBean someFilterRegistration() {
        //这是一种注册方式在每个过滤器中注册
        return registerFilter();
    }

    /**
     * 给指定的过滤器注册
     */
    private FilterRegistrationBean registerFilter() {
        FilterRegistrationBean filterBean = new FilterRegistrationBean();
        //过滤器 自己写的过滤器
        GlobleHandle globleHandle = new GlobleHandle();
        // 设置过滤器名称, 执行顺序, 拦截url
        String filterName = globleHandle.getClass().getSimpleName();
        //过滤器的顺序,越小越先
        int order = 3;

        List<String> urlPatterns = new ArrayList<>(1);
        //拦截url
        urlPatterns.add("/*");

        filterBean.setFilter(globleHandle);
        filterBean.setName(filterName);
        filterBean.setOrder(order);
        filterBean.setUrlPatterns(urlPatterns);

        log.info(String.format("注册过滤器, 过滤器name: %s, 过滤器order: %d, 过滤器过滤url: %s", filterName, order, urlPatterns));
        return filterBean;
    }










    /**
     * 第二种方式
     * 配置过滤器
     * 注册过滤器
     *
     * 两种注册方式大同小异
     *
     */
    @Bean
    public FilterRegistrationBean someFilterRegistration2() {
        return getFilterRegistrationBean();
    }

    /**
     *  第二种方式 start
     */
    private FilterRegistrationBean<Filter> getFilterRegistrationBean() {
        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
        SystemUrlFilter systemUrlFilter = systemUrlFilter();
        //这里就是自定义过滤器
        registrationBean.setFilter(systemUrlFilter);
        registrationBean.addInitParameter("targetFilterLifecycle","true");
        //过滤的地址
        registrationBean.addUrlPatterns("/*");

        registrationBean.setOrder(4);
        registrationBean.setName(systemUrlFilter.getClass().getSimpleName());
        //不过滤的
        registrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.png,*.css,*.ico");
        registrationBean.setDispatcherTypes(DispatcherType.REQUEST);
        log.info(String.format("注册过滤器, 过滤器name: %s, 过滤器order: %d, 过滤器过滤url: %s", "systemUrlFilter", registrationBean.getOrder(), registrationBean.getUrlPatterns()));
        return registrationBean;
    }

    /**
     * 自定义过滤的初始化
     * 这里如果配置bean注解，那么就会注解把过滤器生效
     * @return
     */
    //@Bean(name="systemUrlFilter")
    public SystemUrlFilter systemUrlFilter(){
        return new SystemUrlFilter();
    }
    /* 第二种方式 end*/
}
